<?php

namespace Modules\Order\Entities;

use Modules\Core\Entities\BaseEntity as Model;
use ThrowException;

class Aftersale extends Model
{
    protected $guarded = [];
    protected $table = 'aftersales';

    protected $appends = ['union_status', 'union_status_text'];

    protected $casts = [
        'images' => 'json',
        'sendback_address' => 'json',
        'resend_address' => 'json',
        'sendback_info' => 'json',
        'resend_info' => 'json',
    ];

    public function getUnionStatusTextAttribute($value)
    {
        $map = [
        	'audit_pending' => '审核中',
            'sendback_pending' => '待买家寄回',
            'sendback_received_pending' => '退货中',
            'resend_pending' => '待卖家再次寄出',
            'resend_received_pending' => '待买家再次收货',
            'refund_pending' => '待退款',
        	'completed' => '售后完成',
            'audit_rejected' => '审核不通过',
        	'aborted' => '售后中止',
            'closed' => '关闭',
        ];
        return @v($map[$this->union_status], '未知状态');
    }

    public function getUnionStatusAttribute($value)
    {
        if ($this->status === 0)
            $status = 'audit_rejected';
        elseif ($this->status === 1)
            $status = 'audit_pending';
        elseif ($this->status === 3)
            $status = 'completed';
        elseif ($this->status === 4)
            $status = 'aborted';
        elseif ($this->status === 5)
            $status = 'closed';
        else {
            if ($this->sub_status === 0)
                $status = 'sendback_pending';
            elseif ($this->sub_status === 1)
                $status = 'sendback_received_pending';
            elseif ($this->sub_status === 2)  {
                if ($this->type === 0) // 退货
                    $status = 'refund_pending';
                elseif ($this->type === 1)
                    $status = 'resend_pending';
            }
            elseif ($this->sub_status === 3)
                $status = 'resend_received_pending';
            elseif ($this->sub_status === 4)
                $status = 'completed';
        }

        return $status;
    }

    public function order()
    {
    	return $this->belongsTo('\Modules\Order\Entities\BaseOrder', 'order_id');
    }

    static public function generateNumber()
    {
        $number = date('1His') . rand(100, 999);
        return $number;
    }

    // 使用订单初始化创建售后
    static public function createWith($order, $options)
    {
        $order->update([
            'aftersale_status' => 1
        ]);

        $order->aftersales()->delete();
        
        $info = [
            'order_id' => $order->id,
            'number' => static::generateNumber(),
            'user_id' => $order->user_id,
            'deliver_status' => @$options['deliver_status'],
            'reason' => @$options['reason'],
            'desc' => @$options['desc'],
            'images' => @$options['images'],
        ];

        return static::create($info);
    }

    public function accept($options = [])
    {
        if ($this->deliver_status === 1) {
            // 仅退款
            $this->complete();
        }
        else {
            // 退货退款
            $this->status = 2;
        }

        $this->audited_at = now();
        $this->audit_remark = @$options['remark'];
        $this->save();
    }

    public function reject($options = [])
    {
        $this->status = 0;
        $this->audited_at = now();
        $this->audit_remark = @$options['remark'];
        $this->save();

        $this->order->update([
            'aftersale_status' => 4
        ]);
    }

    // 用户自行关闭
    public function closeByUser()
    {
        $this->status = 5;
        $this->save();
        $this->order->update([
            'aftersale_status' => 3
        ]);
    }

    public function abort()
    {
        $this->status = 4;
        $this->save();
        $this->order->update([
            'aftersale_status' => 3
        ]);
    }

    // 
    public function complete()
    {
        $this->status = 3;
        $this->completed_at = now();
        $this->save();

        $this->order->update([
            'aftersale_status' => 2
        ]);
    }

    public function sendback($info)
    {
        $this->sendback_at = now();
        $this->sendback_info = $info;
        $this->sub_status = 1;
        $this->save();
    }

    public function resend($info)
    {
        $this->resend_at = now();
        $this->resend_info = $info;
        $this->sub_status = 3;
        $this->save();
    }

    public function sendbackReceive()
    {
        $this->sendback_recevied_at = now();
        $this->save();
    }

    public function refund($money = null)
    {
        $this->order->refund($money);
        $this->refunded_at = now();

        $this->sub_status = 4;
        $this->save();

        $this->complete();
    }
}
